package com.zhiwei.crawler;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zhiwei.entity.TiebaData;
import com.zhiwei.httpclient.TiebaHttpClientDemo;

public class GetTiebaByCrawler {
    
    public static void main(String[] args) {
        GetTiebaByCrawler byCrawler = new GetTiebaByCrawler();
//        String cookie = "BIDUPSID=81FD76825BFB1DBC6351266DB123EB21; BAIDUID=9A1384716DCBD51F29BACF1C82002BBE:FG=1; PSTM=1517207658; TIEBA_USERTYPE=f986682cd931e90efd7f2eca; TIEBAUID=cb23caae14130a0d384a57f1; bdshare_firstime=1517536646804; MCITY=-180%3A; wise_device=0; BDSFRCVID=RI8sJeC62ZfQeDbA9DZehrB7l_Autd7TH6aoYyaxrB9O9hMdJCJxEG0PDf8g0Ku-KFK6ogKKKgOTHIjP; H_BDCLCKID_SF=tR4t_K0-fC03fP36q45H24k0-qrtetJyaR3ub-bbWJ5TMCoYypQU5x4FKRo-ttvH0JvQ0lvcbb--ShPC-frsKJK4-q-eKjFtMn6aQ-FK3l02VhbIe-t2ynQDXxKHq4RMW23i0h7mWpT8VKF4D60WD5J0jNRf-b-X2KTXWR5JHJOoDjrnh6RPeM0yyxomtjjxBjTioIQpflbKqnn4L6JSbfPT0R62LUkqKebzWttaaRQzbMnO-TjfjlLqQttjQnohfIkja-5tyn7-qn7TyURvbU47y-rBQTIftJkD_KDMf-3bfTrYhtno-ICShUFsQfFO-2Q-5KL-yJQGeqbPLjrUW4LJK4Kf2pRCb6nb_MbdJJjootTp2b5IefR-M-cbBfJZMgTxoUJg5DnJhhvGqfRPyTKebPRiB-b9QgbABftLK-oj-D89ejD53e; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[iL4hrzJ0zlT]=mbxnW11j9Dfmh7GuZR8mvqV; BDRCVFR[C0p6oIjvx-c]=mbxnW11j9Dfmh7GuZR8mvqV; H_PS_PSSID=1469_21118_20719; PSINO=5; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1518231577,1519973526,1519974110,1520401855; bottleBubble=1; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1520401860";
        String[] s = "剑三+重制版".split(",");
        for(int i = 0;i<s.length;i++) {
            String word = s[i];
            List<TiebaData> lists = new ArrayList<TiebaData>();
            lists = byCrawler.getDataByWord(word, null);
            System.out.println(word);
            for(TiebaData data : lists) {
                System.out.println(data.toString());
            }
//            SaveToMongo.saveToMongoDB(lists, word, "tieba", "剑三 重制版");
            System.out.println(lists.size());
        }
    }
    
    public List<TiebaData> getDataByWord(String word,String cookie) {
        List<TiebaData> lists = new ArrayList<TiebaData>();
        try {
            String url = "http://tieba.baidu.com/f/search/res?qw="+URLEncoder.encode(word,"UTF-8")+"&pn=1";
            Document document = TiebaHttpClientDemo.executeHttpRequestGet(url, cookie);
            lists = getTiebaData(document);
            while(true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                url = "";
                url = document.select("div.pager-search > a.next").attr("href");
                System.out.println(url);
                if("".equals(url)) {
                    break;
                }
                document = TiebaHttpClientDemo.executeHttpRequestGet("http://tieba.baidu.com"+url, cookie);
                lists.addAll(getTiebaData(document));
            }
            
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return lists;
    }
    
    private static List<TiebaData> getTiebaData(Document document) {
        Elements elements = document.select("div.s_main").select("div.s_post_list").select("div.s_post");
        List<TiebaData> lists = new ArrayList<TiebaData>();
        for(Element element : elements) {
            TiebaData tiebaData = new TiebaData();
            String title = element.select("span.p_title").select("a").text();
            tiebaData.setTitle(title);
            String url = element.select("span.p_title").select("a").attr("href");
            tiebaData.setUrl("http://tieba.baidu.com" + url);
            String tid = element.select("span.p_title").select("a").attr("data-tid");
            tiebaData.setTid(tid);
            String content = element.select("div.p_content").text();
            if(content == null) {
                break;
            }
            tiebaData.setContent(content);
            String source = element.select("a.p_forum").select("font.p_violet").text();
            tiebaData.setSource(source);
            String author = element.select("a").select("font.p_violet").text();
            try {
                tiebaData.setAuthor(author.split(" ")[1]);
            }
            catch (Exception e) {
                e.printStackTrace();
                System.out.println("无作者 或者 无来源");
                break;
            }
            String time = element.select("font.p_date").text();
            if(time == null && "".equals(time)) {
                break;
            }
            tiebaData.setTime(time+":00");
            System.out.println(tiebaData.toString());
            lists.add(tiebaData);
        }
        return lists;
    }
    
}
